package com.farsight.leo.fssmartlock.activities;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;

import com.farsight.leo.fssmartlock.R;
import com.farsight.leo.fssmartlock.bases.BaseActivity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class SplashActivity extends BaseActivity {

    private String mPermissions[] = new String[]{
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.READ_EXTERNAL_STORAGE
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestPermissions();
    }

    private void requestPermissions() {
        boolean shouldProceed = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            for (String pem : mPermissions) {
                if (checkSelfPermission(pem) != PackageManager.PERMISSION_GRANTED) {
                    shouldProceed = false;
                    break;
                }
            }
            if (shouldProceed) {
                proceed();
            }else {
                requestPermissions(mPermissions, 9527);
            }
        }else {
            proceed();
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean shouldProceed=true;
        if (requestCode == 9527) {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                    shouldProceed=false;
                    break;
                }
            }
            if(shouldProceed){
                proceed();
            }else {
                finish();
            }
        }

    }

    private void proceed() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                LoginActivity.start(SplashActivity.this);
                finish();
            }
        }, 1000);
    }

    @Override
    protected int set_layout_id() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListeners() {

    }

}
